package com.etalk.crm.serviceImpl;

import com.etalk.crm.dao.ActivityInfoMapper;
import com.etalk.crm.pojo.ActivityInfo;
import com.etalk.crm.pojo.KcPackage;
import com.etalk.crm.service.ActivityInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Auther: James
 * @Date: 2019/2/26 15:06
 * @Description:
 */
@Service
public class ActivityInfoServiceImpl implements ActivityInfoService {
    @Resource
    private ActivityInfoMapper activityInfoMapper;

    @Override
    public PageInfo<ActivityInfo> activityInfoList(Integer page,Integer size, String state, String searchKey) {
        PageHelper.startPage(page, size);
        PageInfo<ActivityInfo> pageInfo = new PageInfo(activityInfoMapper.activityInfoList(state,searchKey));
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        page = pageInfo.getPageNum();
        size = pageInfo.getPageSize();
        return pageInfo;
    }
    @Transactional
    @Override
    public int delActivityInfo(int id) {
        ActivityInfo activityInfo = new ActivityInfo();
        activityInfo.setId(id);
        activityInfo.setState("0");
        return activityInfoMapper.updateActivityInfo(activityInfo);
    }

    @Transactional
    @Override
    public int addActivityInfo(ActivityInfo activityInfo) {
        if(activityInfoMapper.selectById(activityInfo.getId()) != null){
            return -1;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            activityInfo.setStartTime(sdf.parse(activityInfo.getStartDateStr()));
            activityInfo.setEndTime(sdf.parse(activityInfo.getEndDateStr()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return activityInfoMapper.addActivityInfo(activityInfo);
    }

    @Transactional
    @Override
    public int updateActivityInfo(ActivityInfo activityInfo) {
        return activityInfoMapper.updateActivityInfo(activityInfo);
    }

    @Override
    public ActivityInfo selectById(int id) {
        return activityInfoMapper.selectById(id);
    }

    @Override
    public List<KcPackage> selectPackage() {
        return activityInfoMapper.selectPackage();
    }

    @Override
    public List<ActivityInfo> promotionList() {
        return activityInfoMapper.promotionList();
    }

    @Override
    public PageInfo<ActivityInfo> promotionPage(Integer page, Integer size, String state, String searchKey) {
        PageHelper.startPage(page, size);
        PageInfo<ActivityInfo> pageInfo = new PageInfo(activityInfoMapper.promotionForPage(state,searchKey));
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        page = pageInfo.getPageNum();
        size = pageInfo.getPageSize();
        return pageInfo;
    }

    @Override
    public int delPromotion(int id) {
        ActivityInfo activityInfo = new ActivityInfo();
        activityInfo.setId(id);
        activityInfo.setStatus(0);
        return activityInfoMapper.updatePromotion(activityInfo);
    }

    @Override
    public int addPromotion(ActivityInfo activityInfo) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            activityInfo.setStartTime(sdf.parse(activityInfo.getStartDateStr()));
            activityInfo.setEndTime(sdf.parse(activityInfo.getEndDateStr()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return activityInfoMapper.addPromotion(activityInfo);
    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public int updatePromotion(ActivityInfo activityInfo) {
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
       try {
            activityInfo.setStartTime(sdf.parse(activityInfo.getStartDateStr()));
            activityInfo.setEndTime(sdf.parse(activityInfo.getEndDateStr()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return activityInfoMapper.updatePromotion(activityInfo);
    }
}
